
public class GameOfLife {

	EvolvedPopulation population;
	RuleSet rules;
	
	GameOfLife ()
	{
	}
	
	GameOfLife (EvolvedPopulation pop, RuleSet ruleSet)
	{
		init(pop,ruleSet);
	}
	
	private void init(EvolvedPopulation pop, RuleSet ruleSet)
	{
		population = pop;
		rules = ruleSet;
	}
	
	public void reset(EvolvedPopulation pop, RuleSet ruleSet) { init(pop,ruleSet);}
	
	public void runSimulation(int generations)
	{
		int xSize = population.getXSize();
		int ySize = population.getYSize();
		
		for (int generation=0; generation<generations; generation++)
		{
			//bottom left corner
			population.set(0, 0,
					rules.ruleAnswer(
					formKey(population.get(xSize-1,0), 
							population.get(0,1), 
							population.get(1,0), 
							population.get(0,ySize-1),
							population.get(0,0))));
			//top left corner
			population.set(0, ySize-1, 
					rules.ruleAnswer(
					formKey(population.get(xSize-1,ySize-1), 
							population.get(0,0), 
							population.get(1,ySize-1), 
							population.get(0,ySize-2),
							population.get(0,ySize-1))));
			
			//top right
			population.set(xSize-1, ySize-1, 
					rules.ruleAnswer(
					formKey(population.get(xSize-2,ySize-1), 
							population.get(xSize-1,0), 
							population.get(0,ySize-1), 
							population.get(xSize-1,ySize-2),
							population.get(xSize-1,ySize-1))));
			
			//bottom right
			population.set(xSize-1, 0,
					rules.ruleAnswer(
					formKey(population.get(xSize-2,0), 
							population.get(xSize-1,1), 
							population.get(0,0), 
							population.get(xSize-1,ySize-1),
							population.get(xSize-1,0))));
			
			//left side
			for(int j=1; j<ySize-1; j++)
			{
				population.set(0, j,
						rules.ruleAnswer(
						formKey(population.get(xSize-1,j), 
								population.get(0,j+1), 
								population.get(1,j), 
								population.get(0,j-1),
								population.get(0,j))));
				
			}
			//right side
			for(int j=1; j<ySize-1; j++)
			{
				population.set(xSize-1, j,
						rules.ruleAnswer(
						formKey(population.get(xSize-2,j), 
								population.get(xSize-1,j+1), 
								population.get(0,j), 
								population.get(xSize-1,j-1),
								population.get(xSize-1,j))));	
			}
			//bottom side
			for(int i=1; i<xSize-1; i++)
			{
				population.set(i, 0,
						rules.ruleAnswer(
						formKey(population.get(i-1,0), 
								population.get(i,1), 
								population.get(i+1,0), 
								population.get(i,ySize-1),
								population.get(i,0))));
			}
			
			//top side
			for(int i=1; i<xSize-1; i++)
			{
				population.set(i, ySize-1, 
						rules.ruleAnswer(
						formKey(population.get(i-1,ySize-1), 
								population.get(i,0), 
								population.get(i+1,ySize-1), 
								population.get(i,ySize-2),
								population.get(i,ySize-1))));
			}
			
			for(int i=1; i<xSize-1; i++ )
			{
				for(int j=1;j<ySize-1;j++)
				{
					population.set(i, j,
							rules.ruleAnswer(
							formKey(population.get(i-1,j), 
									population.get(i,j+1), 
									population.get(i+1,j), 
									population.get(i,j-1),
									population.get(i,j))));
				}
			}
			population.swapPopulations();
		}
	}
	
	private int formKey(int p1, int p2, int p3, int p4, int p5)
	{
		int ret = (p1*10000) + (p2*1000) + (p3*100) + (p4*10) + p5;
		return ret;
	}
}
